window.Apex = window.Apex || {};
Apex.CTX = contextPath;

Apex.VideoCfg={
	objId:'cif_video_obj_id',
	clsId:'1D5E998B-4B4B-48D8-80DA-588A76E0684E',
	version:'3,0,1,0',
	file:Apex.CTX+'/js/common/plug-in/vedio/AxWebVideoAgent.cab',
	width:0,
	height:0
};
if(browserMatchForApex.browser=="IE"){//支持7--11
	$("body").append('<OBJECT width="1" height="1" ID="' + Apex.VideoCfg.objId +'" width="'+Apex.VideoCfg.width+'" height="'+Apex.VideoCfg.height+'" CLASSID="clsid:' + Apex.VideoCfg.clsId + '" CODEBASE="'+Apex.VideoCfg.file+'#version='+Apex.VideoCfg.version+'"></OBJECT>');
}else{
	$("body").append('<OBJECT width="1" height="1" ID="' + Apex.VideoCfg.objId +'" type="application/webvideoagent" color="ff00ff00" ></OBJECT>');
}

Apex.VideoObj = document.getElementById(Apex.VideoCfg.objId);

function AttachIE11Event(obj, _strEventId, _functionCallback) {
    var nameFromToStringRegex = /^function\s?([^\s(]*)/;
    var paramsFromToStringRegex = /\(\)|\(.+\)/;
    var params = _functionCallback.toString().match(paramsFromToStringRegex)[0];
    var functionName = _functionCallback.name || _functionCallback.toString().match(nameFromToStringRegex)[1];
    var handler;
    try {
        handler = document.createElement("script");
        handler.setAttribute("for", obj.id);
    }
    catch(ex) {
        handler = document.createElement('<script for="' + obj.id + '">');
    }
    handler.event = _strEventId + params;
    handler.appendChild(document.createTextNode(functionName + params + ";"));
    document.body.appendChild(handler);
};

jQuery.fn.video = function(cfg){
	var cfg=jQuery.extend(Apex.VideoCfg,cfg||{});
	if(this&&this.length>0){
		var me = $(this[0]);
		
		var video = jQuery.data(me,"video");
		if(video){
			return video;
		}
		
		var objEl = $("#"+cfg.objId);
		if(objEl.length==0){
			var objElstr='<OBJECT ID="' + Apex.VideoCfg.objId +'" width="'+Apex.VideoCfg.width+'" height="'+Apex.VideoCfg.height+'" CLASSID="clsid:' + Apex.VideoCfg.clsId + '" ></OBJECT>';
			if(browserMatchForApex.browser!="IE"){//支持7--11
				objElstr='<OBJECT ID="' + Apex.VideoCfg.objId +'" type="application/webvideoagent" color="ff00ff00" style="width:340px; height:260px;"></OBJECT>';
			}
			$("#"+cfg.objId).remove();
			$("body").append(objElstr+"");
			Apex.VideoObj = $("#"+Apex.VideoCfg.objId)[0];
			objEl = $("#"+cfg.objId);
		}
		objEl = objEl[0];
		var obj = jQuery(objEl);
		if(cfg.width){
			cfg.width = ""+cfg.width;
			if(cfg.width.indexOf("px")==-1)cfg.width+="px";
			objEl.style.width = cfg.width;
		}
		if(cfg.height){
			cfg.height = ""+cfg.height;
			if(cfg.height.indexOf("px")==-1)cfg.height+="px";
			objEl.style.height = cfg.height;
		}
		this[0].appendChild(objEl);
		
		video = new Apex.video(objEl,obj,cfg.server);
		jQuery.data(me,"video",video);
		return video;
	}
};

Apex.video = function(objEl){
	this.objEl = objEl;
	this.obj = jQuery(objEl);
	this.inited = false;
	this.params={};
};

jQuery.extend(Apex.video, {
	prototype: {
		//公用
		init : function(cfg){
			var cfg = cfg || {};
			this.objEl.SetUserId(cfg.userid+"\0");
			this.objEl.SetUserName(cfg.username+"\0");
			this.objEl.SetWorkgroup(cfg.workgroup+"\0");
			this.objEl.SetServerIp(cfg.server+"\0");
			this.objEl.SetServerPort(parseInt(cfg.port?cfg.port:0));
			this.objEl.SetRemoveVideo(1);
			// 0 无效果 1 随机选 2 不查询列表，改为从服务器查询一个服务器
			this.objEl.SetMediaServerAutoSelectMode(1);
			this.objEl.SetUIMode(1);
			this.objEl.SetBkColor(220, 220, 220);//灰白色
			this.objEl.SetTextColor(0,0,240);//蓝色字体
			this.inited = true;
		},
		attachEvent:function(name,func){
			$.removeEventHandler(this.objEl,name.replace("On",""),func);
			if(browserMatchForApex.browser=="IE"&&browserMatchForApex.version>=11){//IE11处理
				AttachIE11Event(this.objEl,name,func);
			}else if (browserMatchForApex.browser=="IE"&&browserMatchForApex.version<11) {    
	            this.objEl.attachEvent(name,func);    
	        }else if (this.objEl.addEventListener){    
				this.objEl.addEventListener(name.replace("On",""),func,false);
	        }  
		},
		start:function(){
			this.setParam();
			this.objEl.StartAgentChat();
		},
		stop:function(){
			this.objEl.StopAgentChat();
		},
		quit:function(){
			this.objEl.Quit();
		},
		addParam:function(obj,value){
			if("object" == typeof(obj)){
				jQuery.extend(this.params,obj);
			}else{
				this.params[obj] = value;
			}
		},
		setOrg:function(orgId){
			this.objEl.SetQuestionEx(orgId+"\0");
		},
		setUserName:function(userName){
			this.objEl.SetUserName(userName+"\0");
		},
		changeSize:function(width,height){
			var agentctrl = this.objEl;
			$(agentctrl).width(width);
			$(agentctrl).height(height);
		},
		//私有
		setParam:function(){
			var arr = [];
			arr.push('<param>');
			for(var key in this.params){
				var value = this.params[key];
				key = key.toLowerCase();
				if(key==="phototype"){
					arr.push('<'+key+'>');
					for(var i=0;i<value.length;i++){//影像类型数组
						var value2 = value[i];
						arr.push('<item>');
						arr.push('<name>'+value2.YXLXMC+'</name>');
						arr.push('<value>'+value2.YXLX+'</value>');
						arr.push('</item>');
					}
					arr.push('</'+key+'>');
				}else{
					arr.push('<'+key+'>'+value+'</'+key+'>');
				}
			}
			//新版本3.0视频组件升级参数 固定值
			arr.push('<external-video-proxy>vchat</external-video-proxy>');
			arr.push('</param>');
			var param =  arr.join("");
			this.objEl.SetCustomerInfo(param);
		}
	}
});
